#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i)  
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;
using std::priority_queue;
int T,n,a[200000],b[200000],map[30][30],fa[200000];
char s;
struct node{
    int u,v;
    bool operator<(const node &rhs)const{
        return v > rhs.v;
    }
}m[200000];
priority_queue<node> q;

inline int getfa(int x){
    if(fa[x] == x) return x;
    else return fa[x] = getfa(fa[x]);
}

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);
    cin >> T;
    while(T--){
        int ans = 0;
        while(q.size()) q.pop();
        rep(i,1,30) rep(j,1,30) map[i][j] = -1;
        bool flag = true;
        cin >> n;
        rep(i,1,n){
            cin >> s;
            a[i] = s - 'a' + 1;
        }
        rep(i,1,n){
            cin >> s;
            b[i] = s - 'a' + 1;
        }
        rep(i,1,n){
            m[i].u = a[i] , m[i].v = b[i];
            q.push(m[i]);
            if(a[i] > b[i]) flag = false;
            if(!flag) break;
        }
        if(!flag){
            cout << "-1\n";
            continue;
        }
        rep(i,1,26) fa[i] = i;
        while(q.size()){
            node x = q.top();q.pop();
            int uu = x.u , vv = x.v;
            fa[uu] = getfa(fa[uu]) , fa[vv] = getfa(fa[vv]);
            if(fa[uu] != fa[vv]){
                ++ans , fa[fa[uu]] = fa[vv];
            } 
        }
        cout << ans << "\n";
    }
    return 0;
}